package com.landking.exit;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.util.Log;

import com.landking.application.MyApplication;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class ExitAppUtils {
    private List<Activity> mActivityList = new LinkedList<Activity>();
    private static ExitAppUtils instance;

    private ExitAppUtils() {
    }

    ;

    public static ExitAppUtils getInstance() {
        if (null == instance) {
            instance = new ExitAppUtils();
        }
        return instance;
    }


    public void createActivity(Activity activity) {
        mActivityList.add(activity);
    }

    public void destroyActivity(Activity activity) {
        mActivityList.remove(activity);
    }

    public List<Activity> getAllActivity() {
        return mActivityList;
    }

    public void exit() {

        /********退出时断开MQ连接start*********/
		/*try {
			//在常量中取得登陆成功以后存储的MQ连接，断开
    		if(ConstValues.mqttClient!=null){
    			ConstValues.mqttClient.disconnect();
    		}
		} catch (Exception e) {
			Log.e("MQConnection", "MQ断开连接失败或者已经断开", e);
			e.printStackTrace();
		}*/
        /********退出时断开MQ连接end*********/

        for (Activity activity : mActivityList) {
            activity.finish();
        }

        Log.d("BaseActivity", "exit");
        android.os.Process.killProcess(android.os.Process.myPid());    //获取PID
        System.exit(0);   //常规java、c#的标准退出法，返回值为0代表正常退出
    }


    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static String getVersion() {
        try {
            PackageManager manager = MyApplication.getAppContext().getPackageManager();
            PackageInfo info = manager.getPackageInfo(MyApplication.getAppContext().getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "未获取版本号";
        }
    }

    /**
     * 得到用指定方式格式化的系统日期
     *
     * @param iType 返回日期格式
     *              ====================================================
     *              Type       格式
     *              0        20110411(yyyyMMdd)
     *              1        20110411 22:52:08(yyyyMMdd hh:mm:ss)
     *              2		20110411 225208(yyyyMMdd hhmmss)
     *              3        2011.04.11 22:52:08(yyyy.MM.dd hh:mm:ss)
     *              4        2011.04.11(yyyy.MM.dd)
     *              5        22:52:08(hh:mm:ss)
     *              6		2011-04-11  22:52:08(yyyy-MM-dd  hh:mm:ss)
     *              7		2011-04-11 (yyyy-MM-dd)
     *              8		2011-04-11 22:52:08(yyyy-MM-dd HH:mm:ss)
     *              9		20110411225208(yyyyMMddhhmmss)
     *              10		22:52(hh:mm)
     *              11		12月25日 10:50:34
     *              ====================================================
     * @return 日期时间字符串
     */
    public static String getDateTimeStr(int iType) {
        String strResult = "";

        // 实例化数据库交互接口
        /*IDatabaseToolBS atabasetool =
                    (IDatabaseToolBS)new DateTools().getBean("IDatabaseToolBS");*/

        // 取得数据库时间
        try {
            SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");//设置日期格式
            SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");//设置日期格式
            SimpleDateFormat date1 = new SimpleDateFormat("yyyy.MM.dd");//设置日期格式
            SimpleDateFormat date2 = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            SimpleDateFormat date3 = new SimpleDateFormat("MM月dd日");//设置日期格式
            Object[] obj = new Object[]{date.format(new Date()), time.format(new Date()), date1.format(new Date()), date2.format(new Date()), date3.format(new Date())};//atabasetool.getDBNowTime();
            if (iType == 0)
                strResult = obj[0].toString().replaceAll("-", "").trim();
            if (iType == 1)
                strResult = obj[0].toString().replaceAll("-", "").trim() + " " + obj[1].toString();
            if (iType == 2)
                strResult = obj[0].toString().replaceAll("-", "").trim() + obj[1].toString().replaceAll(":", "");
            if (iType == 3)
                strResult = obj[2].toString() + "  " + obj[1].toString();
            if (iType == 4)
                strResult = obj[2].toString();
            if (iType == 5)
                strResult = obj[1].toString();
            if (iType == 6)
                strResult = obj[3].toString() + "  " + obj[1].toString();// yyyy-MM-dd HH:mm:ss
            if (iType == 7)
                strResult = obj[3].toString();
            if (iType == 8)
                strResult = obj[3].toString() + " " + obj[1].toString();// yyyy-MM-dd HH:mm:ss
            if (iType == 9)
                strResult = obj[0].toString().replaceAll("-", "").trim() + obj[1].toString().replaceAll(":", "").trim();
            if (iType == 10)
                strResult = obj[1].toString().substring(0, 5);
            if (iType == 11)
                strResult = obj[4].toString() + " " + obj[1].toString();// yyyy-MM-dd HH:mm:ss

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return strResult;
    }

    /**
     * 把数据写入文件中
     *
     * @param requestData
     * @param filePath
     */
    public static void writeTxt(String requestData, String filePath) {
        String json = requestData + "\n";
        File file = new File(filePath); // 要写入的文件
        BufferedWriter writer = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            writer = new BufferedWriter(new FileWriter(file, true));
            writer.newLine();
            writer.write(json);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    /**
     * 获取sd卡路径
     *
     * @return
     */
    public static String getSDPath() {
        File sdDir = null;
        // 判断sd卡是否存在
        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (sdCardExist) {// 如果SD卡存在，则获取跟目录
            sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
        }
        return sdDir.toString();
    }

    // 崩溃日志文件夹路径 /storage/emulated/0/Android/data/包名/files/bug/
    public static String getBugPath() {
        String path = MyApplication.getAppContext().getExternalFilesDir("bug").getPath() + File.separator;
        return path;
    }

}

